﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;
using Jungle.Code.DAL;
using Jungle.Code.Utils;

namespace Jungle.Code.Core
{
	public class Node
	{
		#region Fields
		private int _nodeID;
		private int _parentNodeID;
		private int _templateID;
		private int _langID;
		private string _alias;
		private string _relativeUrl;
		private DateTime _creationTimeStamp;
		private DateTime _modificationTimeStamp;
		private int _ord;
		private bool _enabled;
		private string _title;
        private string _name;
		private string _keywords;
		private string _description;
		#endregion

		#region Properties
		public int NodeID {
			get {
				return _nodeID;
			}
			set {
				_nodeID = value;
			}
		}
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
		public int ParentNodeID {
			get {
				return _parentNodeID;
			}
			set {
				_parentNodeID = value;
			}
		}
		public int TemplateID {
			get {
				return _templateID;
			}
			set {
				_templateID = value;
			}
		}
		public int LangID {
			get {
				return _langID;
			}
			set {
				_langID = value;
			}
		}
		public string Alias {
			get {
				return _alias;
			}
			set {
				_alias = value;
			}
		}
		public string RelativeUrl {
			get {
				return _relativeUrl;
			}
			set {
				_relativeUrl = value;
			}
		}
		public DateTime CreationTimeStamp {
			get {
				return _creationTimeStamp;
			}
			set {
				_creationTimeStamp = value;
			}
		}
		public DateTime ModificationTimeStamp {
			get {
				return _modificationTimeStamp;
			}
			set {
				_modificationTimeStamp = value;
			}
		}
		public int Ord {
			get {
				return _ord;
			}
			set {
				_ord = value;
			}
		}
		public bool Enabled {
			get {
				return _enabled;
			}
			set {
				_enabled = value;
			}
		}
		public string Title {
			get {
				return _title;
			}
			set {
				_title = value;
			}
		}
		public string Keywords {
			get {
				return _keywords;
			}
			set {
				_keywords = value;
			}
		}
		public string Description {
			get {
				return _description;
			}
			set {
				_description = value;
			}
		}
		#endregion

		public Node() {
			Ord = 0;
		}
		public static Node GetByNodeID(int nodeID) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_GetByNodeID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("nodeID", SqlDbType.Int).Value = nodeID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					Node node = new Node();
					dr.Read();
					SetAllFields(node, dr);
					dr.Close();
					return node;
				}
				dr.Close();
			}
			return null;
		}
		public static List<Node> GetByParentNodeID(int parentNodeID) {
			List<Node> nodes = new List<Node>();
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_GetByParentNodeID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("parentNodeID", SqlDbType.Int).Value = parentNodeID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					while (dr.Read()) {
						Node node = new Node();
						SetAllFields(node, dr);
						nodes.Add(node);
					}
				}
				dr.Close();
			}
			return nodes;
		}
		public static Node GetTopByLangID(int langID) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_GetTopByLangID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("langID", SqlDbType.Int).Value = langID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					dr.Read();
					Node node = new Node();
					SetAllFields(node, dr);
					dr.Close();
					return node;
				}
				dr.Close();
			}
			return null;
		}
		public static Node GetByAlias(string alias) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_GetByAlias", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("alias", SqlDbType.VarChar, 50).Value = alias;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					dr.Read();
					Node node = new Node();
					SetAllFields(node, dr);
					dr.Close();
					return node;
				}
				dr.Close();
			}
			return null;
		}

        public static Node GetByUrl(string url)
        {
            SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
            using (SqlCommand command = new SqlCommand("dbo.crNodes_GetByUrl", conn))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("relativeUrl", SqlDbType.VarChar, 255).Value = url;
                conn.Open();
                SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (dr.HasRows)
                {
                    dr.Read();
                    Node node = new Node();
                    SetAllFields(node, dr);
                    dr.Close();
                    return node;
                }
                dr.Close();
            }
            return null;
        }

		public static bool Update(Node node) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_UpdateByNodeID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("nodeID", SqlDbType.Int).Value = node.NodeID;
				command.Parameters.Add("parentNodeID", SqlDbType.Int).Value = node.ParentNodeID;
				command.Parameters.Add("templateID", SqlDbType.Int).Value = node.TemplateID;
				command.Parameters.Add("alias", SqlDbType.VarChar, 50).Value = node.Alias;
				command.Parameters.Add("relativeUrl", SqlDbType.VarChar, 255).Value = node.RelativeUrl;
				command.Parameters.Add("ord", SqlDbType.Int).Value = node.Ord;
				command.Parameters.Add("enabled", SqlDbType.Bit).Value = node.Enabled;
				command.Parameters.Add("keywords", SqlDbType.Text).Value = node.Keywords;
				command.Parameters.Add("description", SqlDbType.VarChar, 50).Value = node.Description;
				command.Parameters.Add("title", SqlDbType.VarChar, 255).Value = node.Title;
                command.Parameters.Add("name", SqlDbType.VarChar, 255).Value = node.Name;

				conn.Open();
				command.ExecuteNonQuery();
				return true;
			}
		}
		public static bool Insert(Node node) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crNodes_Insert", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("langID", SqlDbType.Int).Value = node.LangID;
				command.Parameters.Add("parentNodeID", SqlDbType.Int).Value = node.ParentNodeID;
				command.Parameters.Add("templateID", SqlDbType.Int).Value = node.TemplateID;
				command.Parameters.Add("alias", SqlDbType.VarChar, 50).Value = node.Alias;
				command.Parameters.Add("relativeUrl", SqlDbType.VarChar, 255).Value = node.RelativeUrl;
				command.Parameters.Add("ord", SqlDbType.Int).Value = node.Ord;
				command.Parameters.Add("enabled", SqlDbType.Bit).Value = node.Enabled;
				command.Parameters.Add("keywords", SqlDbType.Text).Value = node.Keywords;
				command.Parameters.Add("description", SqlDbType.VarChar, 50).Value = node.Description;
				command.Parameters.Add("title", SqlDbType.VarChar, 255).Value = node.Title;
                command.Parameters.Add("name", SqlDbType.VarChar, 255).Value = node.Name;

				conn.Open();
				command.ExecuteNonQuery();
				return true;
			}
		}
		private static void SetAllFields(Node node, SqlDataReader dr) {
			node.NodeID = (int)dr["nodeID"];
			node.ParentNodeID = (int)dr["parentNodeID"];
			node.TemplateID = (int)dr["templateID"];
			node.LangID = (int)dr["langID"];
			node.Alias = (string)dr["alias"];
			node.RelativeUrl = (string)dr["relativeUrl"];
			node.CreationTimeStamp = (DateTime)dr["creationTimeStamp"];
			node.ModificationTimeStamp = (DateTime)dr["modificationTimeStamp"];
			node.Ord = (int)dr["ord"];
			node.Enabled = (bool)dr["enabled"];
			node.Title = (string)dr["title"];
            node.Name = (string)dr["name"];
			if (dr["keywords"] != DBNull.Value)
				node.Keywords = (string)dr["keywords"];
			if (dr["description"] != DBNull.Value)
				node.Description = (string)dr["description"];
		}
        public static string GetFullUrlForNode(int nodeID)
        {
            string url = "";

            Node node = new Node();
            node = Node.GetByNodeID(nodeID);
            url += node._alias;
            while (node._parentNodeID != 0)
            {
                node = Node.GetByNodeID(node.ParentNodeID);
                url = url.Insert(0, node._alias + "/");
            }
            url = url.Insert(0, UrlHelper.GetSiteUrl() + "/");
            
            return url;
        }
	}
}
